#include "interrupt.h"
#include "usart.h"

KEY_InitTypeDef key[4] = {0, 0, 0,};
uint32_t g_time;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim->Instance == TIM4)
    {
        static uint8_t t = 0;
        t++;
        if(g_time != 0 && t == 100)
        {
            g_time--;
            t = 0;
        }
        
        key[0].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
        key[1].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
        key[2].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);
        key[3].key_sta = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
        
        for(uint8_t i = 0; i < 4; i++)
        {
            switch(key[i].judge_sta)
            {
                case 0:
                {
                    if(key[i].key_sta == 0)
                    {
                        key[i].judge_sta = 1;
                    }
                }
                break;
                case 1:
                {
                    if(key[i].key_sta == 0)
                    {
                        key[i].judge_sta = 2;
                        key[i].single_flag = 1;
                    }
                    else
                    {
                        key[i].judge_sta = 0;
                    }
                }
                break;
                case 2:
                {
                    if(key[i].key_sta == 1)
                    {
                        key[i].judge_sta = 0;
                    }
                }
                break;
            }
        }
    }
}

double   ccr_vala = 0;
uint32_t ccr_valb = 0;
uint32_t frq = 0;
float duty = 0;
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    if(htim->Instance == TIM2)
    {
        if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
        {
            ccr_vala = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
            ccr_valb = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
            __HAL_TIM_SetCounter(htim, 0);
            frq = (80000000 / 80) / ccr_vala;
            duty = (ccr_valb / ccr_vala) * 100;
            HAL_TIM_IC_Start(htim, TIM_CHANNEL_1);
            HAL_TIM_IC_Start(htim, TIM_CHANNEL_2);
        }
    }
}

char rx_data[30];
uint8_t rx_dat;
uint8_t rx_pointer;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    rx_data[rx_pointer++] = rx_dat;
    HAL_UART_Receive_IT(&huart1, &rx_dat, 1);
}






